/*
 * Copyright (c) 2013-2018 上海汇数数据. All rights reserved.
 * @(#) DefaultOutVO.java 2018-08-08 17:34
 */

package cn.jh.common.core.ret;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;


/**
 * @author Fuchun
 * @since 1.0
 */
final class DefaultOutVO extends BaseOutVO implements OutVO {

    private static final long serialVersionUID = 1L;

    static DefaultOutVO data(@NotNull Object data) {
        return ok(null, data);
    }

    static DefaultOutVO ok(@Nullable String message, @Nullable Object data) {
        return new DefaultOutVO(Constants.SUCCESS_CODE, message, null, data);
    }

    static <X extends Throwable> DefaultOutVO error(@NotNull X ex, @Nullable String description) {
        String message = ex.getLocalizedMessage() != null ? ex.getLocalizedMessage() : ex.getMessage();
        return new DefaultOutVO(Constants.ERROR_CODE, message, description, null);
    }

    private final String code;
    private final String message;
    private final String description;
    private final Object data;

    DefaultOutVO(String code, String message, String description) {
        this(code, message, description, null);
    }

    private DefaultOutVO(String code, @Nullable String message, @Nullable String description, @Nullable Object data) {
        this.code = code;
        this.message = message;
        this.description = description;
        this.data = data;
    }

    @Override
    public String getCode() {
        return code;
    }

    @Nullable
    @Override
    public String getMessage() {
        return message;
    }

    @Nullable
    @Override
    public String getDescription() {
        return description;
    }

    @Nullable
    @Override
    public Object getData() {
        return data;
    }
}
